package com.cp.core.controller.api;


import com.cp.base.util.JwtUtils;
import com.cp.common.result.R;
import com.cp.common.result.ResponseEnum;
import com.cp.core.pojo.vo.LoginVo;
import com.cp.core.pojo.vo.RegisterVo;
import com.cp.core.pojo.vo.UserInfoVo;
import com.cp.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author ming
 * @since 2022-07-27
 */
@RestController
@RequestMapping("/api/core/userInfo")
@Api(tags = "会员")
@Slf4j
//@CrossOrigin
public class UserInfoController {
    @Resource
    UserInfoService userInfoService;
    @PostMapping("/register")
    public R regist(@RequestBody RegisterVo registerVo){
        log.info("registerVo{}", registerVo);
        userInfoService.register(registerVo);
        return R.ok().message("注册成功");
    }
    @PostMapping("/login")
   public R login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        log.info("loginVo{}",loginVo);
        UserInfoVo userInfoVo = userInfoService.login(loginVo, request);
        return R.ok().data("userInfoVo",userInfoVo).message("登录成功");
   }
    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        boolean boo = JwtUtils.checkToken(token);
        if(boo){
            return R.ok();
        }else{
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }
    @ApiOperation("校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable String mobile){
        return userInfoService.checkMobile(mobile);
    }
}

